package com.manthano.security.user.service;

import java.util.UUID;

import org.springframework.security.acls.domain.GrantedAuthoritySid;
import org.springframework.security.core.GrantedAuthority;

/**
 * @author LXMRX
 */
public class Role implements GrantedAuthority, Comparable<GrantedAuthority>
{

	private static final long	serialVersionUID	= 1L;
	private final String		roleId;
	private String				name;
	private String				description;

	/**
	 * 
	 */
	public Role()
	{
		this.roleId = UUID.randomUUID().toString();
	}

	/**
	 * @param roleId
	 */
	public Role(String roleId)
	{
		this.roleId = roleId;
	}

	/**
	 * @param roleId
	 * @param displayName
	 * @param description
	 */
	public Role(String roleId, String displayName, String description)
	{
		this.roleId = roleId;
		this.name = displayName;
		this.description = description;
	}

	@Override
	public String getAuthority()
	{
		return roleId;
	}

	/**
	 * @return
	 */
	public String getName()
	{
		return name;
	}

	/**
	 * @param name
	 */
	public void setName(String name)
	{
		this.name = name;
	}

	/**
	 * @return description
	 */
	public String getDescription()
	{
		return description;
	}

	/**
	 * @param description
	 */
	public void setDescription(String description)
	{
		this.description = description;
	}

	@Override
	public int compareTo(GrantedAuthority o)
	{
		if (o.getAuthority() == null || getAuthority() == null)
			return 0;
		if (o instanceof Role)
			return roleId.compareTo(((Role) o).roleId);
		return o.getAuthority().compareTo(getAuthority());
	}

	@Override
	public boolean equals(Object obj)
	{
		if (obj == null)
			return false;

		if (this == obj)
			return true;

		if (obj instanceof Role)
			return roleId.equals(((Role) obj).roleId);
		if (obj instanceof GrantedAuthority)
			return ((GrantedAuthority) obj).getAuthority().equals(getAuthority());
		if (obj instanceof GrantedAuthoritySid)
			return ((GrantedAuthoritySid) obj).getGrantedAuthority().equals(getAuthority());
		return false;
	}

	@Override
	public int hashCode()
	{
		return roleId.hashCode();
	}

	@Override
	public String toString()
	{
		return "Role [roleId=" + roleId + ", name=" + name + ", description=" + description + "]";
	}

}
